<template>
  <div class="modelHandleAdd" v-loading="isLoading">
    <el-dialog
      title="事件交办"
      :visible.sync="eventDialogVisible"
      width="1150px"
      height="800px"
      :modal="false"
      :before-close="handleClose"
      :close-on-click-modal="false"
      custom-class="deep"
    >
      <el-form
        :model="formData"
        :rules="formRules"
        ref="elForm"
        label-width="100px"
        class="event-platform"
      >
        <el-form-item label="事件标题" prop="title">
          <el-input type="textarea" v-model="formData.title" :autosize="{ minRows: 1, maxRows: 8 }"></el-input>
        </el-form-item>
        <el-form-item label="事件描述" prop="desc">
          <el-input type="textarea" v-model="formData.desc" :autosize="{ minRows: 1, maxRows: 8 }" ></el-input>
        </el-form-item>
        <el-form-item label="发生时间" required>
          <el-col :span="6">
            <el-form-item prop="date1">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="formData.date1"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="1" style="text-align: center">-</el-col>
          <el-col :span="6">
            <el-form-item prop="date2">
              <el-time-picker
                placeholder="选择时间"
                v-model="formData.date2"
                style="width: 100%"
                value-format="HH:mm:ss"
              ></el-time-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="发生地点" required prop="name">
          <el-input
            type="input"
            disabled
            v-model="formData.name"
            placeholder="请选择发生地点"
            style="width: 85%"
          ></el-input>
          <el-button
            type="primary"
            class="ml-5"
            v-show="!mapShow"
            @click="mapShow = !mapShow"
            >选择</el-button
          >
          <el-button
            type="danger"
            class="ml-5"
            v-show="mapShow"
            @click="mapShow = !mapShow"
            >关闭</el-button
          >
          <div class="map" v-show="mapShow" style="width: 100%; height: 300px">
            <el-amap-search-box
              id="pickerInput"
              class="search-box"
              :search-option="searchOption"
              :on-search-result="onSearchResult"
            ></el-amap-search-box>
            <el-amap
              vid="amapDemo"
              :center="mapCenter"
              zoom="14"
              :events="events"
              class="amap-demo"
            >
              <el-amap-marker
                v-for="marker in markers"
                :position="marker"
                :key="marker.index"
              ></el-amap-marker>
            </el-amap>
          </div>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="涉及人数" prop="peopleNum">
              <el-input
                v-model="formData.peopleNum"
                placeholder="请输入人数"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="等级分类" prop="region1">
              <el-select
                v-model="formData.region1"
                placeholder="请选择等级分类"
              >
                <el-option label="特级" value="1"></el-option>
                <el-option label="紧急" value="2"></el-option>
                <el-option label="一般" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="重点场所" prop="isEmphasis">
              <el-select
                v-model="formData.isEmphasis"
                placeholder="请选择是否是重点场所"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="事件类型" prop="type">
              <el-cascader
                v-model="formData.type"
                :options="typeOptions"
                placeholder="选择事件类型"
                @change="typeCascaderChange"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="办结时间" prop="value1">
              <el-date-picker
                v-model="formData.value1"
                type="date"
                style="width: 100%"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="交办层级" prop="region">
              <el-cascader
                v-model="formData.region"
                :options="regionOptions"
                @change="regionCascaderChange"
                :props="{ checkStrictly: true, emitPath: false }"
                placeholder="选择交办层级"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="20" v-if="BMselectShow">
            <el-form-item>
              <el-select
                v-model="formData.value"
                multiple
                filterable
                placeholder="请选择"
                style="width: 100%"
                @change="handleSelectChange"
              >
                <el-option
                  v-for="item in BUOptions"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" class="checkBox" v-if="BMselectShow">
            三人处置小组
            <el-checkbox
              v-model="formData.checked"
              class="elCheckBox"
              @change="checkChange"
            ></el-checkbox>
          </el-col>
          <el-col :span="6">
            <el-form-item label="附件">
              <el-upload
                multiple
                class="upload-demo"
                :action="baseURL + 'society/olap/file'"
                :on-success="UploadSuccess"
                :on-remove="handleRemove"
                :file-list="fileList"
              >
                <el-button type="text">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('elForm')">提交</el-button>
          <el-button @click="onClear('elForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getQyyu,
  getAllBuMen,
  getEventFlowGridsTqjbTypes,
  wrningAssignFlowSecond,
} from "../../utils/api";
// import util from '@/utils/util.js';
// src\utils\util.js
export default {
  name: "handleEventDialog",
  // props: {
  //   eventSource: Object,
  // },
  data() {
    return {
      eventSource: {},
      isLoading: false,
      eventDialogVisible: false,
      formData: {
        title: "",
        desc: "",
        name: "",
        region1: "3",
        isEmphasis: "0",
        files: [],
        checked: false,
      },
      operate: "flowToPingan", // 保存时传的参数2
      formRules: {
        title: [{ required: true, message: "请填写事件标题", trigger: "blur" }],
        desc: [{ required: true, message: "请填写事件描述", trigger: "blur" }],
        date1: [
          {
            type: "string",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "string",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        name: [{ required: true, message: "请选择发生地点", trigger: "blur" }],
        region1: [
          { required: true, message: "请选择等级分类", trigger: "change" },
        ],
        isEmphasis: [
          {
            required: true,
            message: "请选择是否为重点场所",
            trigger: "change",
          },
        ],
        type: [
          { required: true, message: "请选择事件类型", trigger: "change" },
        ],
        value1: [
          {
            type: "string",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        peopleNum: [{ pattern: /^\d{1,}$/, message: "数字必须为正整数" }],
        region: [
          { required: true, message: "请选择处置区域", trigger: "change" },
        ],
      },
      typeOptions: [],
      regionOptions: [],
      BUOptions: [],
      BMselectShow: false,
      fileList: [],
      // map
      mapShow: false,
      mapCenter: [118.954291, 28.903396],
      markers: [],
      pickerInput: "",
      // mapStyle: 'amap://styles/d6154f75de146edc5e225254ba84b535',
      events: {
        init: () => {
          console.log("初始化高德地图1");
        },
        click: (e) => {
          document.querySelector("#pickerInput").__vue__.keyword = "";
          let { lng, lat } = e.lnglat;
          this.markers = [];
          this.markers.push([lng, lat]);
          const AMap = window.AMap;
          const self = this;
          AMap.plugin(["AMap.Geocoder"], function () {
            var geocoder = new window.AMap.Geocoder({
              radius: 1000,
              extensions: "all",
            });
            geocoder.getAddress([lng, lat], function (status, result) {
              if (status === "complete" && result.info === "OK") {
                if (result && result.regeocode) {
                  self.formData.name = result.regeocode.formattedAddress;
                  console.log(
                    result.regeocode.formattedAddress,
                    self.formData.name
                  );
                }
              }
            });
          });
        },
      },
      searchOption: {
        city: "衢江区",
        citylimit: false,
        input: "pickerInput",
      },
      resultData: { isThreeHandle: 0, flowPingAn: { files: [] } },
    };
  },
  computed: {
    baseURL() {
      return `http://${window.document.location.host}/`;
    },
  },
  methods: {
    handleEventDialogOpen() {
      this.eventDialogVisible = true;
    },
    onSearchResult(pois) {
      if (pois.length !== 0) {
        const { lat, lng, name } = pois[0];
        this.formData.name = name;
        this.markers = [];
        this.markers.push([lng, lat]);
        this.mapCenter = [lng, lat];
      }
    },
    handleClose() {
      this.eventDialogVisible = false;
      this.$emit("close");
    },
    regionCascaderChange(value) {
      if (value === "330803000000") {
        // 选择在区
        this.BMselectShow = true;
        getAllBuMen().then((res) => {
          if (res.data.success) {
            this.BUOptions = res.data.data[0].children;
          }
        });
      }
    },
    typeCascaderChange(value) {
      if (value.length > 1) {
        this.resultData.flowPingAn.firstCategoryId = value[0];
        this.resultData.flowPingAn.secondCategoryId = value[1];
      }
    },
    UploadSuccess(response) {
      if (response.success) {
        this.resultData.flowPingAn.files.push(response.data);
      } else {
        this.$message.error("上传失败");
      }
    },
    handleRemove(file) {
      const fileId = file.response.data.fileId;
      const index = this.resultData.flowPingAn.files.findIndex(
        (v) => v.fileId === fileId
      );
      this.resultData.flowPingAn.files.splice(index, 1);
      console.log(this.resultData.flowPingAn.files);
    },
    handleSelectChange(value) {
      this.resultData.flowPingAn.deptStr = value.join(",");
      if (value.length === 0) {
        this.formData.checked = false;
        this.resultData.isThreeHandle = 0;
      }
    },
    checkChange(value) {
      console.log(this.formData.value, value);
      if (this.formData.value.length === 0) {
        this.$message.error("请选择处置部门");
        this.formData.checked = false;
        this.resultData.isThreeHandle = 0;
      } else {
        this.resultData.isThreeHandle = this.formData.checked ? 1 : 0;
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.setResultDate();
        }
      });
    },
    setResultDate() {
      this.resultData.flowPingAn.eventSubject = this.formData.title;
      this.resultData.flowPingAn.eventContent = this.formData.desc;
      this.resultData.flowPingAn.eventDate =
        this.formData.date1 + " " + this.formData.date2;
      this.resultData.flowPingAn.eventLocation = this.formData.name;
      this.resultData.flowPingAn.longiTude = this.markers[0][0];
      this.resultData.flowPingAn.latiTude = this.markers[0][0];
      this.resultData.flowPingAn.relatePeopleCount = this.formData.peopleNum;
      // 获取等级分类存入搜索条件中
      this.resultData.flowPingAn.eventLevel = this.formData.region1;
      // 获取是否重点场所存入搜索条件中
      this.resultData.flowPingAn.isImpplace = this.formData.isEmphasis;
      this.resultData.flowPingAn.shuntLimitDate = this.formData.value1;
      this.resultData.flowPingAn.orgInternalCode = this.formData.region;
      this.assignFlowSecond();
    },
    onClear(formName) {
      console.log(formName);
      this.eventDialogVisible = false;
      this.$emit("close");
    },
    assignFlowSecond() {
      wrningAssignFlowSecond(this.resultData, this.operate).then((res) => {
        if (res.status === 200) {
          this.$message({
            message: "事件交办提交成功！",
            type: "success",
          });
          this.$emit("close");
          this.eventDialogVisible = false;
        } else {
          this.$message({
            message: res.msgs[Object.keys(res.msgs)[0]],
            type: "error",
          });
        }
      });
    },
    passhandle(info) {
      this.eventSource = info;
      if (this.eventSource.source === "矛调协同平台") {
        this.formData.title = '"基层治理四平台-云智衢江"';
      } else {
        const content =
          this.eventSource.eventDesc || this.eventSource.warnContent;
        this.formData.title = '"基层治理四平台-云智衢江"' + content;
      }
      if (this.eventSource.reportTime) {
        this.formData.date1 = this.eventSource.reportTime.slice(0, 10);
        this.formData.date2 = this.eventSource.reportTime.slice(11);
      } else {
        this.formData.date1 = this.eventSource.createTime.slice(0, 10);
        this.formData.date2 = this.eventSource.createTime.slice(11);
      }
      this.formData.desc =
        this.eventSource.eventDesc || this.eventSource.warnContent;

      this.formData.value1 = util.getDateStr(6);
    },
  },
  mounted() {
    window.handleEventDialogInit = this.handleEventDialogOpen;
    window.passhandleInit = this.passhandle;
    getQyyu().then((res) => {
      if (res.data.success) {
        this.regionOptions = res.data.data[0].children;
      }
    });
    getEventFlowGridsTqjbTypes().then((res) => {
      if (res.data.success) {
        this.typeOptions = res.data.data;
      }
    });
  },
};
</script>

<style>
.event-platform {
  margin-right: 50px;
}
.el-dialog__wrapper {
  width: 3096px;
  overflow: unset !important;
}
.el-form{
  margin-left: 40px;
}
.event-platform .el-form-item__label {
  color: #fff;
  font-size: 18px !important;
}
.event-platform .el-textarea__inner,
.event-platform .el-input__inner {
  background-color: transparent;
  /* background: linear-gradient(0deg, rgba(22, 46, 79, 0.7), rgba(5, 13, 35, 0.7)); */
  border: 1px solid #43ccff;
  box-shadow: 0px 0px 4px 0px rgba(2, 16, 45, 0.46);
  border-radius: 1px;
  color: #fff;
  font-size: 18px !important;
  height: 35px;
  line-height: 1.8;
}
</style>

<style  scoped>
.event-platform {
  max-height: 500px;
  overflow-y: auto;
}
.event-platform::-webkit-scrollbar {
  width: 6px;
}

.event-platform::-webkit-scrollbar-thumb {
  background-color: #43ccff;
  border-radius: 3px;
}
.map {
  position: relative;
}
.map .search-box {
  position: absolute;
  top: 10px;
  left: 20px;
  z-index: 99;
  width: 40%;
}
.checkBox {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
</style>
